package cn.nexd.www.demo;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import cn.nexd.collector.NexdCollector;
import cn.nexd.collector.NexdCollectorAgent;
import cn.nexd.collector.bean.BeaconScannerResult;
import cn.nexd.collector.bean.MixCollectorResult;
import cn.nexd.collector.bean.WifiCollectorResult;
import cn.nexd.collector.listener.BeaconCollectorListener;
import cn.nexd.collector.listener.MixCollectorListener;
import cn.nexd.collector.listener.WifiCollectorListener;
import cn.nexd.location.indoor.NexdIndoorLocation;
import cn.nexd.location.indoor.NexdIndoorLocationAgent;
import cn.nexd.location.indoor.NexdIndoorLocationConfiguration;
import cn.nexd.location.indoor.NexdIndoorLocationResult;
import cn.nexd.location.indoor.listener.NexdIndoorLocationListener;
import cn.nexd.location.indoor.utils.AndroidUtil;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private NexdCollectorAgent nexdCollectorAgent;
    private Button stopButton;
    private Button startButton;
    private Button location;
    private NexdIndoorLocationAgent nexdIndoorLocationAgent;
    private String path = "";
    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        context = this;
        nexdCollectorAgent = NexdCollectorAgent.getCollectorAgent(context);
        //        NexdCollectorConfiguration.Buidler buidler = new NexdCollectorConfiguration.Buidler();
        //        buidler.setAppkey("");
        //        buidler.setBeaconCollectorDelay(100);
        //        buidler.setWifiCollectorDelay(100);
        //        buidler.setBluetoothCollectorRate(1500);
        //        buidler.setBluetoothCollectorEnable(true);
        //        buidler.setWifiCollectorEnable(true);
        //        buidler.setWifiCollectorRate(1500);
        //
        //        nexdCollectorAgent = NexdCollectorAgent.getCollectorAgent(this);
        //        nexdCollectorAgent.setCollectorConfiguration(buidler.build());
        //        nexdCollectorAgent.setBeaconCollectorListener(new DefaultBeaconCollectorListener());
        //        nexdCollectorAgent.setWifiCollectorListener(new DefaultWifiCollectorListener());
        //        nexdCollectorAgent.setMixCollectorListener(new DefaultMixCollectorListener());
        startButton = (Button) findViewById(R.id.start_button);
        startButton.setOnClickListener(this);
        stopButton = (Button) findViewById(R.id.stop_button);
        stopButton.setOnClickListener(this);

        location = (Button) findViewById(R.id.location);
        location.setOnClickListener(this);

        nexdIndoorLocationAgent = NexdIndoorLocationAgent.getIndoorLocationAgent(this);
        NexdIndoorLocationConfiguration.Buidler locationBuidler = new NexdIndoorLocationConfiguration.Buidler();
        locationBuidler.setAppkey("");
        locationBuidler.setLocationMode(NexdIndoorLocationConfiguration.LocationMode.LOCATION_MODE_BEACON_ONLY);
        locationBuidler.setLocationRate(1200);
        nexdIndoorLocationAgent.setIndoorLocationConfiguration(locationBuidler.build());
        nexdIndoorLocationAgent.setIndoorLocationListener(new NexdIndoorLocationListener() {
            @Override
            public void onLocationStart(long buildingId, long floorId, NexdIndoorLocationConfiguration.LocationMode locationMode) {
                Log.d("location_test", "onLocationStart  " + buildingId + " " + floorId + " " + locationMode);
            }

            @Override
            public void onLocationSuccess(NexdIndoorLocationResult indoorLocationResult) {
                Log.d("location_test", "onLocationSuccess" + indoorLocationResult.toString());
            }

            @Override
            public void onLocationFailed(NexdIndoorLocation.LocationState locationState) {
                Log.d("location_test", "onLocationFailed" + locationState.toString());
            }

            @Override
            public void onLocationInterrupt(NexdIndoorLocation.LocationState locationState) {
                Log.d("location_test", "onLocationInterrupt" + locationState.toString());

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        path = AndroidUtil.getDiskCacheDir(this, "nexdLocation");

    }

    @Override
    public void onClick(View v) {
        if (v.getId() == startButton.getId()) {
            if (!nexdCollectorAgent.getIsCollectorRunning()) {
                nexdCollectorAgent.startCollector();
            }
        } else if (v.getId() == stopButton.getId()) {
            if (nexdCollectorAgent.getIsCollectorRunning()) {
                nexdCollectorAgent.stopCollector();
            }
        } else if (v.getId() == location.getId()) {
            File file = new File(NexdIndoorLocation.getConfigurationPath(context, (long) 10108058) + "10108058.mean");
            //            File file = new File(path + "/location/10108106/10108106.mean");
            if (!file.exists()) {
                //不存在
                copyAssets("10108058", NexdIndoorLocation.getConfigurationPath(context, (long) 10108058));
            }
            nexdIndoorLocationAgent.startLocation(10108058, 101080580001L);
            //            nexdIndoorLocationAgent.startLocation(10108106L);
            //            nexdIndoorLocationAgent.startLocation(10108106L, 101081060001L);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    protected void onStop() {
        super.onStop();
        nexdIndoorLocationAgent.stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        nexdCollectorAgent.destroyCollector();
    }

    private class DefaultWifiCollectorListener implements WifiCollectorListener {
        @Override
        public void onCollectionStart() {
            Log.d("test_collector_wifi", "onCollectionStart");
        }

        @Override
        public void onCollectionSuccess(List<WifiCollectorResult> collectorResults) {
            Log.d("test_collector_wifi", "onCollectionSuccess  size   " + collectorResults.size() + "  " + (collectorResults.size() > 0 ? collectorResults.get(0).toString() : ""));

        }

        @Override
        public void onCollectorFailed(NexdCollector.CollectorState collectorState) {
            Log.d("test_collector_wifi", "onCollectorFailed  " + collectorState);

        }

        @Override
        public void onCollectorInterrupt(NexdCollector.CollectorState collectorState) {

            Log.d("test_collector_wifi", "onCollectorInterrupt  " + collectorState);
        }
    }

    private class DefaultBeaconCollectorListener implements BeaconCollectorListener {
        @Override
        public void onCollectionStart() {
            Log.d("test_collector_beacon", "onCollectionStart");
        }

        @Override
        public void onCollectionSuccess(List<BeaconScannerResult> collectorResults) {
            Log.d("test_collector_beacon", "onCollectionSuccess  size  " + collectorResults.size() + " " + (collectorResults.size() > 0 ? collectorResults.get(0).toString() : " "));
        }

        @Override
        public void onCollectorFailed(NexdCollector.CollectorState collectorState) {
            Log.d("test_collector_beacon", "onCollectorFailed  " + collectorState);
        }

        @Override
        public void onCollectorInterrupt(NexdCollector.CollectorState collectorState) {
            Log.d("test_collector_beacon", "onCollectorInterrupt  " + collectorState);
        }
    }

    private class DefaultMixCollectorListener implements MixCollectorListener {

        @Override
        public void onCollectionStart() {
            Log.d("test_collector_mix", "onCollectionStart");
        }

        @Override
        public void onCollectionSuccess(List<MixCollectorResult> collectorResults) {
            Log.d("test_collector_mix", "onCollectionSuccess  size  " + collectorResults.size() + " " + (collectorResults.size() > 0 ? collectorResults.get(0).toString() : " "));
        }

        @Override
        public void onCollectorFailed(NexdCollector.CollectorState collectorState) {
            Log.d("test_collector_mix", "onCollectorFailed  " + collectorState);
        }

        @Override
        public void onCollectorInterrupt(NexdCollector.CollectorState collectorState) {
            Log.d("test_collector_mix", "onCollectorInterrupt  " + collectorState);
        }
    }

    private void copyAssets(String assetDir, String dir) {
        String[] files;
        try {
            files = this.getResources().getAssets().list(assetDir);
        } catch (IOException e1) {
            return;
        }
        File mWorkingPath = new File(dir);
        //if this directory does not exists, make one.
        if (!mWorkingPath.exists()) {
            if (!mWorkingPath.mkdirs()) {

            }
        }

        for (int i = 0; i < files.length; i++) {
            try {
                String fileName = files[i];
                //we make sure file name not contains '.' to be a folder.
                if (!fileName.contains(".")) {
                    if (0 == assetDir.length()) {
                        copyAssets(fileName, dir + fileName + "/");
                    } else {
                        copyAssets(assetDir + "/" + fileName, dir + fileName + "/");
                    }
                    continue;
                }
                File outFile = new File(mWorkingPath, fileName);
                if (outFile.exists())
                    outFile.delete();
                InputStream in = null;
                if (0 != assetDir.length())
                    in = getAssets().open(assetDir + "/" + fileName);
                else
                    in = getAssets().open(fileName);
                OutputStream out = new FileOutputStream(outFile);

                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }

                in.close();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
